﻿using SAIM.Models.BLL;
using SAIM.Models.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace SAIM.Models
{
    public class TicketsViewModel
    {
        public int current { get; set; }
        public int rowCount { get; set; }
        public int total { get; set; }
        public List<userTicketModel> rows;

        public TicketsViewModel(List<TicketsDAL> tickets, int _current = 1, int _rowCount = 10)
        {
            current = _current;
            rowCount = _rowCount;
            total = tickets.Count;
            rows = new List<userTicketModel>();
            foreach (TicketsDAL ticket in tickets)
            {
                rows.Add(new userTicketModel()
                {
                    area = ticket.Issues.ParentIssue != null ? ticket.Issues.ParentIssue.Name : string.Empty,
                    issue = ticket.Issues.Name,
                    ticketDate = ticket.TicketDate.ToString(),
                    userName = ticket.AspNetUsers.UserName,
                    details = ticket.Details,
                    status = "",
                    Id = ticket.Id,
                    Closed = ticket.Closed
                });
            }
        }
    }

    public class NewTicketsViewModel
    {
        private List<IssuesDAL> _Areas;
        private List<IssuesDAL> _Issues;
        private List<AspNetUsersDAL> _Users;

        [Display(Name = "Solicitante")]
        public string User { get; set; }

        [Required]
        [Display(Name = "Área del ticket")]
        public long Area { get; set; }

        [Required]
        [Display(Name = "Servicio")]
        public long Issue { get; set; }

        public IEnumerable<SelectListItem> Areas
        {
            get { return new SelectList(_Areas, "Id", "Name"); }
        }

        public IEnumerable<SelectListItem> Issues
        {
            get { return new SelectList(_Issues, "Id", "Name"); }
        }

        public IEnumerable<SelectListItem> Users
        {
            get { return new SelectList(_Users, "Id", "UserName"); }
        }

        [Required]
        [Display(Name = "Detalle")]
        public String Details { get; set; }

        /// <summary>
        /// Gets a default model for New Ticket View
        /// </summary>
        public NewTicketsViewModel()
        {
            using (IssuesBLL issues = new IssuesBLL())
            {
                IssuesDAL rootIssue = issues.Value.Find(i => i.ParentId == null); // Gets the root Issue
                _Areas = issues.Value.FindAll(i => i.ParentId == rootIssue.Id); // Gets all the Issues that have as a parent to the root Issue
                if (_Areas.Count > 0)
                    _Issues = issues.Value.FindAll(i => i.ParentId == _Areas.ToArray()[0].Id); // Gets all the Issues that have as a parent the first Issue
                else
                    _Issues = new List<IssuesDAL>();
            }
            _Users = new List<AspNetUsersDAL>();
        }

        public NewTicketsViewModel(List<AspNetUsersDAL> usersList)
        {
            using (IssuesBLL issues = new IssuesBLL())
            {
                IssuesDAL rootIssue = issues.Value.Find(i => i.ParentId == null); // Gets the root Issue
                _Areas = issues.Value.FindAll(i => i.ParentId == rootIssue.Id); // Gets all the Issues that have as a parent to the root Issue
                _Areas.Sort((a, b) => a.Name.CompareTo(b.Name));
                if (_Areas.Count > 0)
                    _Issues = issues.Value.FindAll(i => i.ParentId == _Areas.ToArray()[0].Id); // Gets all the Issues that have as a parent the first Issue
                else
                    _Issues = new List<IssuesDAL>();
            }
            _Users = usersList;
            _Users.Sort((a, b) => a.UserName.CompareTo(b.UserName));
        }

        /// <summary>
        /// Gets a specific model for New Ticket View with a Parent Issue and their respectives Sub-Issues
        /// </summary>
        /// <param name="IdParent">Id of a specific Issue</param>
        public NewTicketsViewModel(long IdParent)
        {
            using (IssuesBLL issues = new IssuesBLL())
            {
                IssuesDAL rootIssue = issues.Value.Find(i => i.ParentId == null); // Gets the root Issue
                _Areas = issues.Value.FindAll(i => i.ParentId == rootIssue.Id); // Gets all the Issues that have as a parent to the root Issue
                _Issues = issues.Value.FindAll(i => i.ParentId == IdParent); // Gets all the Issues that have as a parent to the specified Issue
            }
            _Users = new List<AspNetUsersDAL>();
        }
    }

    public class DefaultFileTicketsViewModel
    {

    }

    public class CloseTicketViewModel
    {
        [Required]
        public long IdTicket { get; set; }
    }

    public class GetEditionsByCommentViewModel
    {
        public TicketComment ParentComment;
        public List<TicketComment> Comments;

        public GetEditionsByCommentViewModel(ICollection<TicketCommentEditionsDAL> editions)
        {
            bool firsElement = true;

            Comments = new List<TicketComment>();
            foreach (var element in editions)
            {
                if (firsElement)
                {
                    ParentComment = new TicketComment()
                    {
                        Comment = element.TicketComments.Details,
                        CommentDate = element.TicketComments.CommentDate,
                        IdComment = element.TicketComments.Id,
                        IdTicket = element.TicketComments.TicketId,
                        IsPrivate = element.TicketComments.IsPrivate,
                        UserId = element.TicketComments.UserId,
                        UserName = element.TicketComments.AspNetUsers.UserName

                    };
                    firsElement = !firsElement;
                }
                Comments.Add(new TicketComment()
                {
                    Comment = element.Details,
                    CommentDate = element.CommentDate,
                    IdComment = element.Id, // Id of comment edition
                    IdTicket = element.TicketComments.TicketId,
                    IsPrivate = element.TicketComments.IsPrivate,
                    UserId = element.TicketComments.UserId,
                    UserName = element.TicketComments.AspNetUsers.UserName
                });
            }
            Comments.Reverse();
        }
    }

    public class AttachmentsTicketsViewModel
    {
        #region User data
        public string UserName { get; set; }
        /// <summary>
        /// User Id
        /// </summary>
        public string RPE { get; set; }
        /// <summary>
        /// User email
        /// </summary>
        public string Email { get; set; }
        /// <summary>
        /// User profile
        /// </summary>
        public string Profile { get; set; }
        #endregion

        #region Ticket data
        public long IdTicket { get; set; }
        /// <summary>
        /// Area of this ticket
        /// </summary>
        public string Area { get; set; }
        /// <summary>
        /// Ticket Issue
        /// </summary>
        public string Issue { get; set; }
        /// <summary>
        /// Creation date of ticket
        /// </summary>
        public DateTime TicketDate { get; set; }
        public string Status { get; set; }
        public bool Closed { get; set; }
        /// <summary>
        /// First comment of ticket
        /// </summary>
        public string Details { get; set; }
        #endregion

        public AttachmentsTicketsViewModel(TicketsDAL ticket)
        {
            UserName = ticket.AspNetUsers.UserName;
            RPE = ticket.AspNetUsers.Id;
            Email = ticket.AspNetUsers.Email;
            Profile = ticket.AspNetUsers.AspNetProfiles != null ? ticket.AspNetUsers.AspNetProfiles.Name : "";
            IdTicket = ticket.Id;
            Area = ticket.Issues.ParentIssue.Name;
            Issue = ticket.Issues.Name;
            TicketDate = ticket.TicketDate;
            Status = ticket.TicketStatus;
            Closed = ticket.Closed;
            Details = ticket.Details;
        }
    }

    public class GetFileEditionsViewModel
    {
        public List<TicketAttachment> attachments;

        public GetFileEditionsViewModel(List<TicketAttachmentEditionsDAL> fileEditions)
        {
            attachments = new List<TicketAttachment>();
            
            foreach(TicketAttachmentEditionsDAL edition in fileEditions)
            {
                double fileSize = edition.Attachments.FileSize;
                attachments.Add(new TicketAttachment()
                {
                    IdTicket = edition.TicketsAttachments.IdTicket.ToString(),
                    CreationDate = edition.Attachments.CreationDate.ToString(),
                    FileName = edition.Attachments.Name,
                    FileSize = fileSize < 1000000 ? (fileSize / 1000).ToString("N2").Replace(',', '.') + " KB" : (fileSize / 1000000).ToString("N2").Replace(',', '.') + " MB",
                    HasEditions = edition.Attachments.HasEditions ?? false,
                    Mask = edition.Attachments.Mask,
                    UserName = edition.Attachments.AspNetUsers.UserName
                });
            }
        }

        public GetFileEditionsViewModel(List<ListAttachmentEditionsDAL> fileEditions)
        {
            attachments = new List<TicketAttachment>();

            foreach (ListAttachmentEditionsDAL edition in fileEditions)
            {
                double fileSize = edition.Attachments.FileSize;
                attachments.Add(new TicketAttachment()
                {
                    CreationDate = edition.Attachments.CreationDate.ToString(),
                    FileName = edition.Attachments.Name,
                    FileSize = fileSize < 1000000 ? (fileSize / 1000).ToString("N2").Replace(',', '.') + " KB" : (fileSize / 1000000).ToString("N2").Replace(',', '.') + " MB",
                    HasEditions = edition.Attachments.HasEditions ?? false,
                    Mask = edition.Attachments.Mask,
                    UserName = AspNetUsersBLL.FindUser(edition.Attachments.UserId, string.Empty).Find(a => a.Id == edition.Attachments.UserId).UserName
                });
            }
        }
    }

    public class TicketsUsersViewModel
    {
        public string Name { get; set; }
        [Required]
        [Display(Name = "UPN del usuario")]
        public string UPN { get; set; }
        [Required]
        [Display(Name = "Id del ticket")]
        public long TicketId { get; set; }
    }

    #region clases auxiliares

    public class userTicketModel
    {
        public string area { get; set; }
        public string issue { get; set; }
        public string ticketDate { get; set; }
        public string userName { get; set; }
        public string details { get; set; }
        public string status { get; set; }
        public long Id { get; set; }
        public bool Closed { get; set; }
    }

    public class TicketData
    {
        #region User data
        public string UserName { get; set; }
        /// <summary>
        /// User Id
        /// </summary>
        public string RPE { get; set; }
        /// <summary>
        /// User email
        /// </summary>
        public string Email { get; set; }
        /// <summary>
        /// User profile
        /// </summary>
        public string Profile { get; set; }
        #endregion

        #region Ticket data
        public long IdTicket { get; set; }
        /// <summary>
        /// Area of this ticket
        /// </summary>
        public string Area { get; set; }
        /// <summary>
        /// Ticket Issue
        /// </summary>
        public string Issue { get; set; }
        /// <summary>
        /// Creation date of ticket
        /// </summary>
        public DateTime TicketDate { get; set; }
        public string Status { get; set; }
        public bool Closed { get; set; }
        /// <summary>
        /// First comment of ticket
        /// </summary>
        public string Details { get; set; }
        #endregion

        #region Ticket comments
        public List<TicketComment> Comments;
        #endregion

        #region Fields for new comment
        /// <summary>
        /// Field used to add a comment
        /// </summary>
        [Required(ErrorMessage = "El campo Comentario es obligatorio")]
        public string Comment { get; set; }
        [Required]
        public bool IsPrivate { get; set; }
        [Required]
        public bool ToggleStatus { get; set; }
        [Required]
        public bool HasAttachedFiles { get; set; }
        #endregion

        public TicketData(TicketsDAL ticket)
        {
            UserName = ticket.AspNetUsers.UserName;
            RPE = ticket.AspNetUsers.Id;
            Email = ticket.AspNetUsers.Email;
            Profile = ticket.AspNetUsers.AspNetProfiles != null ? ticket.AspNetUsers.AspNetProfiles.Name : "";
            IdTicket = ticket.Id;
            Area = ticket.Issues.ParentIssue.Name;
            Issue = ticket.Issues.Name;
            TicketDate = ticket.TicketDate;
            Status = ticket.TicketStatus;
            Closed = ticket.Closed;
            Details = ticket.Details;
            Comments = new List<TicketComment>();
            foreach (TicketCommentsDAL element in ticket.TicketComments)
            {
                Comments.Add(
                    new TicketComment()
                    {
                        IdTicket = element.TicketId,
                        Comment = element.Details,
                        CommentDate = element.CommentDate,
                        IdComment = element.Id,
                        IsPrivate = element.IsPrivate,
                        UserId = element.UserId,
                        UserName = element.AspNetUsers.UserName,
                        EditionsCount = element.TicketCommentEdition.Count,
                        CanEdit = element.CanEdit
                    }
                    );
            }
        }
    }

    public class TicketComment
    {
        [Required]
        public long IdTicket { get; set; }
        [Required]
        public string Comment { get; set; }
        [Required]
        public bool IsPrivate { get; set; }
        [Required]
        public bool ToggleStatus { get; set; }
        [Required]
        public bool HasAttachedFiles { get; set; }
        public long IdComment { get; set; }
        public string UserId { get; set; }
        public DateTime CommentDate { get; set; }
        public string UserName { get; set; }
        public int EditionsCount { get; set; }
        public bool CanEdit { get; set; }
    }

    public class TicketAttachment
    {
        public string IdTicket { get; set; }
        public string FileName { get; set; }
        public string Mask { get; set; }
        public string FileSize { get; set; }
        public string CreationDate { get; set; }
        public string UserName { get; set; }
        public bool HasEditions { get; set; }
    }

    public class UserTypeaheadModel
    {
        public string UPN { get; set; }
        public string Name { get; set; }
    }

    public class GridTicketAttachmentsModel
    {
        public int current { get; set; }
        public int rowCount { get; set; }
        public int total { get; set; }
        public List<TicketAttachment> rows;

        public GridTicketAttachmentsModel(List<AttachmentsDAL> attachments, int _current = 1, int _rowCount = 10)
        {
            current = _current;
            rowCount = _rowCount;
            total = attachments.Count;
            rows = new List<TicketAttachment>();
            foreach (AttachmentsDAL attachment in attachments)
            {
                double fileSize = attachment.FileSize;
                rows.Add(new TicketAttachment()
                {
                    Mask = attachment.Mask,
                    FileName = attachment.Name,
                    FileSize = fileSize < 1000000 ? (fileSize / 1000).ToString("N2").Replace(',', '.') + " KB" : (fileSize / 1000000).ToString("N2").Replace(',', '.') + " MB",
                    CreationDate = attachment.CreationDate.ToString(),
                    UserName = attachment.AspNetUsers.UserName,
                    HasEditions = attachment.HasEditions ?? false
                });
            }
        }
    }

    public class GridTicketUsersModel
    {
        public int current { get; set; }
        public int rowCount { get; set; }
        public int total { get; set; }
        public List<TicketsUsersViewModel> rows;

        public GridTicketUsersModel()
        {
            rows = new List<TicketsUsersViewModel>();
        }
    }

    #endregion 
}